package com.cysd.ewmksbs.ui.activity

import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import com.cszy.ewmxns.base.BaseVMActivity

import com.cysd.ewmksbs.viewmodel.MainViewModel
import com.hnyyac.ad.mds.IAdView
import com.hnyyac.ad.mds.IAdViewListener
import com.hnyyac.ad.mds.manager.AdManager
import com.hnyyac.ad.mds.manager.AdManager.AdLoadListener
import com.csylhl.ewmksb.databinding.ActivityWifiConnctionProgressBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


class Ksb_WifiConnctionProgressActivity : BaseVMActivity<MainViewModel, ActivityWifiConnctionProgressBinding>() ,IAdViewListener{

    private var progressJob: Job? = null
    private var isPaused = false
    private var currentProgress = 0
    private val progressChannel = Channel<Unit>() // 用于恢复信号
    var type_proces: String? = null //功能入口类型
    override fun initView(savedInstanceState: Bundle?) {

    }

    override fun initData() {

        startProgressWithPauses()
    }
    private fun startProgressWithPauses() {
        progressJob?.cancel() // 取消之前的任务

        progressJob = CoroutineScope(Dispatchers.Main).launch {
            (1..100).forEach { progress ->
                // 等待恢复信号（如果暂停）
                if (isPaused) {
                    progressChannel.receive() // 挂起直到收到继续信号
                }

                currentProgress = progress
                updateProgress(progress)
                delay(50) // 5秒总时长

                // 检查暂停点
                when (progress) {
                    40 -> showVideo()
                    80 -> showCHApin()
                    100 -> goResult() // 进度完成监听
                }
            }
        }

    }

    private fun updateProgress(progress: Int) {
          mBinding.progressBar.progress = progress

    }
    //激励视频
    private fun showVideo() {
        type_proces="激励视频"
        isPaused = true
        AdManager.loadVideo_nocache(
            this,
            2,
            this,
            object : AdLoadListener {
                override fun onAdLoadFailed(ad: IAdView?) {
                    isPaused = false
                    progressChannel.trySend(Unit) // 发送继续信号
                }
            })
    }
    //插屏
    private fun showCHApin() {
        type_proces="插屏"
        isPaused = true


        AdManager.loadChapin_nocache(
            this,
            2,
            this,
            object : AdLoadListener {
                override fun onAdLoadFailed(ad: IAdView?) {
                    isPaused = false
                    progressChannel.trySend(Unit) // 发送继续信号
                }
            })
    }
    private fun goResult(){
        //跳转到系统设置界面
        val intent = Intent(Settings.ACTION_WIFI_SETTINGS)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        startActivity(intent)
        finish()
    }

    override fun onTrigger(adView: IAdView) {

    }

    override fun onAdShow(adView: IAdView) {

    }

    override fun onAdClick(adView: IAdView) {

    }

    override fun onAdClose(adView: IAdView) {
        isPaused = false
        progressChannel.trySend(Unit) // 发送继续信号
    }

    override fun onError(msg: String) {

    }

    override fun onLoadError(adView: IAdView) {

    }

    override fun onSuccess(adView: IAdView) {

    }

    override fun onLoad(adView: IAdView) {

    }

    override fun onSkipped() {

    }

    override fun onNoAd() {

    }

    override fun onTimeOut() {

    }
    override fun onDestroy() {
        super.onDestroy()
        progressJob?.cancel()
    }
}